#include "SliderListWidget.h"
#include "ui_SliderListWidget.h"

SliderListWidget::SliderListWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SliderListWidget),
    m_currentIndex()
{
    ui->setupUi(this);

    setupUI();

    //activeButton(m_currentIndex);
}

SliderListWidget::~SliderListWidget()
{
    delete ui;
}

int SliderListWidget::count()
{
    return m_btnList.size();
}

int SliderListWidget::currentIndex()
{
    return m_currentIndex;
}

QToolButton* SliderListWidget::currentButton()
{
    return m_btnList[m_currentIndex];
}

void SliderListWidget::next()
{
    int index = (m_currentIndex + 1) % m_btnList.size();
    setCurrentIndex(index);
}

void SliderListWidget::prev()
{
    int index = (m_currentIndex + m_btnList.size() - 1) % m_btnList.size();
    setCurrentIndex(index);
}

void SliderListWidget::setCurrentIndex(int index)
{
    if (index >= 0 && index < m_btnList.size())
    {
        m_currentIndex = index;

        activeButton(m_currentIndex);
    }
}

void SliderListWidget::setupUI()
{
    m_btnList.append(ui->toolButtonBle);
    m_btnList.append(ui->toolButtonContrast);
    m_btnList.append(ui->toolButtonDay);
    m_btnList.append(ui->toolButtonIcon);
    m_btnList.append(ui->toolButtonNight);
    m_btnList.append(ui->toolButtonNightSee);
    m_btnList.append(ui->toolButtonLanguage);
    m_btnList.append(ui->toolButtonPip);
    m_btnList.append(ui->toolButtonRegulate);
    m_btnList.append(ui->toolButtonReset);
    m_btnList.append(ui->toolButtonResolution);
    m_btnList.append(ui->toolButtonTypes);
    m_btnList.append(ui->toolButtonWifi);
    m_btnList.append(ui->toolButtonZoomstep);

    m_btnImageList.append(ButtonImageSet(":/images/ble_large", ":/images/ble_middle", ":/images/ble_small"));
    m_btnImageList.append(ButtonImageSet(":/images/contrast_large", ":/images/contrast_middle", ":/images/contrast_small"));
    m_btnImageList.append(ButtonImageSet(":/images/day_large", ":/images/day_middle", ":/images/day_small"));
    m_btnImageList.append(ButtonImageSet(":/images/icon_large", ":/images/icon_middle", ":/images/icon_small"));
    m_btnImageList.append(ButtonImageSet(":/images/language_large", ":/images/language_middle", ":/images/language_small"));
    m_btnImageList.append(ButtonImageSet(":/images/night_large", ":/images/night_middle", ":/images/night_small"));
    m_btnImageList.append(ButtonImageSet(":/images/nightsee_large", ":/images/nightsee_middle", ":/images/nightsee_small"));
    m_btnImageList.append(ButtonImageSet(":/images/pip_large", ":/images/pip_middle", ":/images/pip_small"));
    m_btnImageList.append(ButtonImageSet(":/images/regulate_large", ":/images/regulate_middle", ":/images/regulate_small"));
    m_btnImageList.append(ButtonImageSet(":/images/reset_large", ":/images/reset_middle", ":/images/reset_small"));
    m_btnImageList.append(ButtonImageSet(":/images/resolution_large", ":/images/resolution_middle", ":/images/resolution_small"));
    m_btnImageList.append(ButtonImageSet(":/images/types_large", ":/images/types_middle", ":/images/types_small"));
    m_btnImageList.append(ButtonImageSet(":/images/wifi_large", ":/images/wifi_middle", ":/images/wifi_small"));
    m_btnImageList.append(ButtonImageSet(":/images/zoomstep_large", ":/images/zoomstep_middle", ":/images/zoomstep_small"));

    makeRects();
}

void SliderListWidget::activeButton(int index)
{
    makeRects();

    for (int i = 0; i < m_btnList.size(); i++)
    {
        int distance = getDistance(i, index, m_btnList.size());

        if (abs(distance) <= 2)
        {
            int pos = distance + 2;
            QRect rc = m_rects[pos];

            m_btnList[i]->setGeometry(rc);
            m_btnList[i]->setIconSize(rc.size());

            ButtonImageSet images = m_btnImageList[i];
            QString imagePath = images.get(distance);
            QIcon icon(imagePath);

            m_btnList[i]->setIcon(icon);

            m_btnList[i]->show();
            //m_btnList[i]->setChecked(true);
        }
        else
        {
            m_btnList[i]->hide();
            m_btnList[i]->setChecked(false);
        }
    }
}

int SliderListWidget::getDistance(int y, int target, int size)
{
    int distance = y - target;
    if (distance > size/2)
    {
        distance -= size;
    }
    else if (distance < (-size/2))
    {
        distance += size;
    }
    return distance;
}

void SliderListWidget::makeRects()
{
    QRect rc = rect();
    //qDebug("rc (%d,%d), (%d,%d)", rc.top(), rc.left(), rc.width(), rc.height());

    QPoint ptCenter = rc.center();

    QRect rcLarge = makeRect(ptCenter, WIDTH_LARGE, HEIGHT_LARGE);
    QRect rcMiddle = makeRect(ptCenter, WIDTH_MIDDLE, HEIGHT_MIDDLE);
    QRect rcMiddle2 = makeRect(ptCenter, WIDTH_MIDDLE, HEIGHT_MIDDLE);
    QRect rcSmall = makeRect(ptCenter, WIDTH_SMALL, HEIGHT_SMALL);
    QRect rcSmall2 = makeRect(ptCenter, WIDTH_SMALL, HEIGHT_SMALL);

    int xOffset = WIDTH_LARGE - WIDTH_MIX;
    rcMiddle.translate(xOffset, 0);
    rcMiddle2.translate(-xOffset, 0);

    xOffset += WIDTH_MIDDLE - WIDTH_MIX;
    rcSmall.translate(xOffset, 0);
    rcSmall2.translate(-xOffset, 0);

    m_rects[0] = rcSmall2;
    m_rects[1] = rcMiddle2;
    m_rects[2]= rcLarge;
    m_rects[3] = rcMiddle;
    m_rects[4] = rcSmall;
}

QRect SliderListWidget::makeRect(const QPoint& ptCenter, int width, int height)
{
    QRect rc;
    rc.setTop(ptCenter.y() - height/2);
    rc.setBottom(ptCenter.y() + height/2);
    rc.setLeft(ptCenter.x() - width/2);
    rc.setRight(ptCenter.x() + width/2);
    return rc;
}
